package com.netease.homework.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.netease.homework.dao.IUserDao;
import com.netease.homework.meta.User;
import com.netease.homework.service.ILoginService;

/**
 * 
 * @author smart
 * @时间 2019年3月8日
 * @描述 登录接口及视图
 */
@Controller
public class loginCtrl {

	@Autowired
	@Qualifier("LoginService")
	ILoginService loginService;

	@Autowired
	IUserDao UserDao;

	/**
	 * 
	 * @描述 登录/登出页
	 * @param request
	 * @return
	 */
	@RequestMapping("/log*")
	public String loginMap(HttpServletRequest request) {
		
		HttpSession session = request.getSession(false);
		if (session!=null) {
			if (session.getAttribute("user")!=null) {
				session.removeAttribute("user");

			}
		}
		return "login";
	}

	/**
	 * 
	 * @描述 登录
	 * @param request
	 * @param name
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/login/login", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request, String name, String password) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {

			Map<String, Object> map = loginService.login(name, password);
			// 获取user实体
			Object object = map.get("value");
			if (object != null) {
				User user = (User) object;
				HttpSession session = request.getSession(false);
				if (session == null) {
					session = request.getSession(true);
				}
				session.setAttribute("user", user);
			}
			returnMap.put("value", object);
			returnMap.put("message", map.get("message"));
			returnMap.put("success", map.get("success"));
		} catch (Exception e) {
			returnMap.put("message", "异常：登录失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}

	/**
	 * 
	 * @描述 注册
	 * @param request
	 * @param name
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/login/register", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> register(HttpServletRequest request, String name, String password) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> map = loginService.register(name, password);
			// 获取user实体
			Object object = map.get("value");
			if (object != null) {
				User user = (User) object;
				HttpSession session = request.getSession();
				session.setAttribute("userName", user.getName());
			}
			returnMap.put("value", object);
			returnMap.put("message", map.get("message"));
			returnMap.put("success", map.get("success"));
		} catch (Exception e) {
			returnMap.put("message", "异常：注册失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}

	/**
	 * 
	 * @描述 从session中获取已登录用户 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/login/getSession", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getSession(HttpServletRequest request) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		User user = null;
		try {

			HttpSession session = request.getSession(false);
			if (session != null) {

				if (session.getAttribute("user") != null) {
					user = (User)session.getAttribute("user");
					
				}
			}
			returnMap.put("value", user);
			returnMap.put("message", "成功");
			returnMap.put("success", "true");
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}
}
